The present invention relates to computer systems, and more particularly to a mechanism for managing the lock and unlock operations on objects.
Threading is a feature of object oriented computer languages such as the Java programming language. A thread is defined as a single sequential flow of control in a computer program. Multiple threads running in a single program allow the program to perform multiple tasks at the same time. Because a thread comprises a sequential flow of control, each thread must reserve some of its own resources within the running program.
Because each thread will reserve resources, programming languages such as Java provide mechanisms for serializing critical sections of the program code. These mechanisms include synchronized methods and synchronized statements where locking and unlocking is done at the object level. An object of this form is termed a monitor. Java includes a set of rules for object locking and unlocking that must be adhered to. As will be familiar to those skilled in the art these rules and other considerations include:
(1) Language defined exceptions are thrown if a thread attempts to lock/unlock an object reference that is NULL or if a thread attempts to unlock an object that it does not hold a lock on.
(2) A thread can do nested locks on an object. The object is only available for other threads to lock if the same number of nested unlocks are done. Thus, some form of bookkeeping must be maintained since a thread can, at any time, relinquish control of the object to another thread and later acquire control of the object at a later time with the same lock count in place.
(3) The Java language defines some rules with respect to the transmission of values across threads when synchronization is used. This requires special considerations particularly in a multi-processor environment.
(4) A thread can do unstructured and uncontrolled object locking and unlocking by using certain native programming interfaces (in particular, JNI monitorEnter( ) and monitorExit( ). The JNI Detachthread( ) API native interface also states that all objects locked by the calling thread must be unlocked.
As a result of these requirements, synchronization may take a substantial part of an application""s time. This is even true of an application that is single threaded since the core Java libraries do object locking and unlocking in commonly used classes.
Furthermore, it may also be desirable to keep track of those objects locked by a thread, in certain environments. One such example is a debugging environment.
Accordingly, there remains a need for a mechanism for managing synchronization, i.e. locking and unlocking of objects, in Java.
The present invention provides a mechanism for managing the locking and unlocking of objects in an object oriented computing environment.
The mechanism according to the invention reduces the execution overhead of the initial and first few nested lock and unlock operations on an object. The mechanism is also suitable for deep nested locking, even though such locking is infrequently used in typical programming.
The mechanism according to the present invention is suitable for single and multi-threaded environments and different locking patterns such as structured and unstructured locking.
Advantageously, the mechanism utilizes minimal space in an object to support the improved locking and unlocking operations. Object space is typically at a premium, and it is therefore desirable to reduce the amount of space that must be dedicated in each object in support of fast locking and unlocking.
The mechanism according to the invention comprises what is termed as typed monitors and a set of operations which can be performed on the monitors. A monitor of a given type is enabled by a monitor transition vector. The operations which can be performed on the typed monitors provide for further execution and space optimization.
According to the invention, a monitor transition vector is assigned to an object when the object is first created. The monitor transition vector comprises a set of monitor transition elements which are ordered. Each transition element, in turn, comprises two transition functions: a locking function and an unlocking function. The locking function is invoked in response to a request to lock the object, and the unlocking function is invoked in response to an unlock request. Each transition function will assign a new transition element to the object, i.e. assign a new element of the transition vector which will be used for the next lock or unlock request. Each transition function may also update preselected information contained in the object or in the address space.
The monitor transition vector links the locking and unlocking operations and provides implicit lock counts through the assignment of transition elements. This improves the execution performance by eliminating the need to perform tests to determine if the synchronization comprises a first lock, a shallow lock or a deeply nested lock.
According to another aspect of the invention, a transition vector is provided for structured locking and another vector is provided for unstructured locking. Each of the transition vectors defines the object""s locking type or class and includes its own set of transition functions. The transition functions define the set of operations that are performed in response to lock and unlock requests.
When the object is first created, a structured transition vector is assigned to the object. If it is later determined that a particular lock or unlock request is unstructured, the type of the object is changed to unstructured by modifying the transition vector associated with the object.
In a first aspect, the present invention provides a mechanism for managing the locking and unlocking of an object by a thread, said mechanism comprising: (a) a transition vector having a plurality of ordered transition elements; (b) a reference element for referencing one of the transition elements in said transition vector, and said reference element being accessible in the object; (c) each of said transition elements including locking means for locking the object and unlocking means for unlocking the object; (d) said locking means having means for assigning said reference element to the next transition element in response to a locking request for the object by the thread; and (e) said unlocking means having means for assigning said reference element to the previous element in response to an unlocking request for the object by the thread.
In a second aspect, the present invention provides a method for managing the locking and unlocking of an object by a thread, said method utilizing a transition vector comprising an ordered set of transition elements and each of said transition elements comprising a structure having first and second function pointers, said first function pointer referencing a locking function for locking said object and said second function pointer referencing an unlocking function for unlocking said object, said method comprising the steps of: (a) assigning a transition vector to an object when the object is created, wherein a reference element in the object is assigned to the first transition element in the transition vector; (b) invoking the locking function in the currently referenced transition element in response to a request from the thread to lock the object, and assigning said reference element to the next transition element in the transition vector; (c) invoking the unlocking function in the currently assigned transition element in response to a request from the thread to unlock the object, and assigning said reference element to the previous transition element in the transition vector; (d) generating an exception in step (c) if a request is made by a thread to unlock an object which has not been locked.
In a third aspect, the present invention provides an article of manufacture in a data storage media recorded with a computer program which, in combination with a general purpose computer loaded with an operating system and runtime library means, equipped to read into memory and execute program data from the data storage media, constituting a method for managing the locking and unlocking of an object by a thread, said method utilizing a transition vector comprising an ordered set of transition elements and each of said transition elements comprising a structure having first and second function pointers, said first function pointer referencing a locking function for locking said object and said second function pointer referencing an unlocking function for unlocking said object, said method comprising the steps of: (a) assigning a transition vector to an object when the object is created, wherein a reference element in the object is assigned to the first transition element in the transition vector; (b) invoking the locking function in the currently referenced transition element in response to a request from the thread to lock the object, and assigning said reference element to the next transition element in the transition vector; (c) invoking the unlocking function in the currently assigned transition element in response to a request from the thread to unlock the object, and assigning said reference element to the previous transition element in the transition vector; (d) generating an exception in step (c) if a request is made by a thread to unlock an object which has not been locked.